using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace Shweet.SPSolution.Features.Shweet_Web
{


    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("c46b2a1d-7f17-468e-b89c-a4f81c76f624")]
    public class Shweet_WebEventReceiver : SPFeatureReceiver
    {
        private const string LISTINSTANCENOTCREATED = "SharePoint should have created the list instance";
        // Uncomment the method below to handle the event raised after a feature has been activated.
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            Contract.Assume(properties != null);
            Contract.Assume(properties.Feature.Parent is SPWeb, "Web Scoped Features assumed to always provide SPWeb as Feature.Parent");
            
            //Add fields to the list
            var web = (SPWeb)properties.Feature.Parent;

            SPList shweetList = web.Lists.TryGetList("Shweets");
            Contract.Assert(shweetList != null, LISTINSTANCENOTCREATED);
            AddShweetFields(shweetList);

            SPList shweetTeamsList = web.Lists.TryGetList("ShweetTeams");
            Contract.Assert(shweetTeamsList != null, LISTINSTANCENOTCREATED);
            AddShweetTeamFields(shweetTeamsList);
        }

        private static void AddShweetFields(SPList shweetList)
        {
            SPField title = shweetList.Fields.TryGetFieldByStaticName("Title");
            Contract.Assert(title != null);
            title.Title = "ShweetText";
            title.Update();

            shweetList.Fields.Add("Tag01", SPFieldType.Text, false);
            shweetList.Fields.Add("Tag02", SPFieldType.Text, false);
            shweetList.Fields.Add("Tag03", SPFieldType.Text, false);
            shweetList.Fields.Add("Tag04", SPFieldType.Text, false);
            shweetList.Fields.Add("Tag05", SPFieldType.Text, false);
            shweetList.Fields.Add("User", SPFieldType.Text, true);
            shweetList.Fields.Add("ShweetedOn", SPFieldType.DateTime, true);
            shweetList.Update();
        }

        private static void AddShweetTeamFields(SPList shweetTeamsList)
        {
            shweetTeamsList.Fields.Add("Url", SPFieldType.URL, true);
            shweetTeamsList.Update();
        }
    }
}
